.TH std::function::target 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::function::target \- std::function::target

.SH Synopsis
   template< class T >               \fB(1)\fP \fI(since C++11)\fP
   T* target() noexcept;
   template< class T >               \fB(2)\fP \fI(since C++11)\fP
   const T* target() const noexcept;

   Returns a pointer to the stored callable function target.

.SH Parameters

   \fI(none)\fP

.SH Return value

   A pointer to the stored function if target_type() == typeid(T), otherwise a null
   pointer.

.SH Example


// Run this code

 #include <functional>
 #include <iostream>

 int f(int, int) { return 1; }
 int g(int, int) { return 2; }
 void test(std::function<int(int, int)> const& arg)
 {
     std::cout << "test function: ";
     if (arg.target<std::plus<int>>())
         std::cout << "it is plus\\n";
     if (arg.target<std::minus<int>>())
         std::cout << "it is minus\\n";

     int (*const* ptr)(int, int) = arg.target<int(*)(int, int)>();
     if (ptr && *ptr == f)
         std::cout << "it is the function f\\n";
     if (ptr && *ptr == g)
         std::cout << "it is the function g\\n";
 }

 int main()
 {
     test(std::function<int(int, int)>(std::plus<int>()));
     test(std::function<int(int, int)>(std::minus<int>()));
     test(std::function<int(int, int)>(f));
     test(std::function<int(int, int)>(g));
 }

.SH Output:

 test function: it is plus
 test function: it is minus
 test function: it is the function f
 test function: it is the function g

   Defect reports

   The following behavior-changing defect reports were applied retroactively to
   previously published C++ standards.

      DR    Applied to        Behavior as published              Correct behavior
   LWG 2591 C++11      the behavior is undefined if T is    behavior is defined (always
                       not Callable                         returns nullptr)

.SH See also

   target_type obtains the typeid of the stored target
               \fI(public member function)\fP
